* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file sets up the dataset used for the regression analysis of domestic market outcomes.

* From Do File #P6a
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_imputation_vars_prepare_jc_multi_d_si4_1865_dro.dta", clear

* From Do File #P7
merge 1:1 cvrnrA aar aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_imputation_vars_prepare_jc_multi_d_si4_1865_AT_dro.dta", keepusing(nprodAT nprodAT_nodup lnnprodAT_nodup nprodT) keep(match) nogen

* Merge with firm-level data
rename cvrnrA cvrnr
rename aar aar_ori
rename aarA aar
merge 1:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", keep(match master) nogen
rename aar aarA
rename aar_ori aar
rename cvrnr cvrnrA

* Drop non-acquirers not in manufacturing nor raw material extraction industries
gen BC = (CVR_BRANCHE_DB07>=100000 & CVR_BRANCHE_DB07<340000)
drop if acquirer== 0 & BC==0

* Drop if merger event occurs before 1999 (not enough years for pre-merger period)
drop if aar<1999 & acquirer==1

* Drop if acquirer has no merger event in remaining sample
egen meanaar = mean(aar), by(cvrnrA)
drop if meanaar==. & acquirer==1

* Drop if acquirer has multiple merger events
egen sdaar = sd(aar), by(cvrnrA)
drop if sdaar>0 & sdaar!=.

* Calculate minimum number of employees in sample
egen minemployee_cvrnr = min(employee_cvrnr) if nprod_dro>0 & nprod_dro!=., by(cvrnrA)

* Calculate minimum number of products in sample
egen minnprod = min(nprod_dro) if nprod_dro>0 & nprod_dro!=., by(cvrnrA)

* Additional calculations
replace d_yr = aarA - meanaar

gen p4 = (d_yr>3 & acquirer==1)
gen m4 = (d_yr<-3 & acquirer==1)

* Calculate change in number of products (for Appendix Table A.16)
gen _x = nprod_dro if aar==aarA
egen mean_x = mean(_x), by(cvrnrA)
gen dnprod = nprod_dro - mean_x

* Define placebo years (for Appendix Figure A.9)
gen d_yrS = aarA - meanaar + 3
gen p4S = (d_yrS>3 & acquirer==1)
gen m4S = (d_yrS<-3 & acquirer==1)
* Indicator for each Indexvalue, except 0 (reference year)
foreach i of numlist 1(1)3{
	gen m`i'S=0
	replace m`i'S=1 if d_yrS==-`i'
	}
foreach i of numlist 1(1)3 {
	gen p`i'S=0
	replace p`i'S=1 if d_yrS==`i'
	}

qui tab aarA, gen(D_AA_)
qui tab aar, gen(D_A_)

* Define spells for imputation estimator
gsort cvrnrA -aarA
by cvrnrA: carryforward aar, gen(aar2)
sort cvrnrA aarA
by cvrnrA: carryforward aar2, gen(aar3)
replace aar3 = aar3+1

compress
save "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\regression_data_domestic.dta", replace
